package com.gzy.xuexiziliao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.mapper.AdminMapper;
import com.gzy.xuexiziliao.pojo.entity.Admin;

import com.gzy.xuexiziliao.service.AdMinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdMinServiceImpl implements AdMinService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(Admin admin) {

        return  adminMapper.login(admin);
    }

    @Override
    public void save(Admin admin) {
        admin.setCreateTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.save(admin);
    }

    @Override
    public Admin getById(Integer id) {
        return adminMapper.getById(id);
    }

    @Override
    public void update(Admin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.update(admin);
    }

    @Override
    public PageResult page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行查询
        List<Admin> adminList=adminMapper.list(name, gender, begin, end);
        Page<Admin> p=(Page<Admin>) adminList;

        //3.封装对象
        PageResult pageResult=new PageResult(p.getTotal(),p.getResult());
        return pageResult;

    }

    @Override
    public void delete(List<Integer> ids) {
        adminMapper.delete(ids);
    }

    @Override
    public List<Admin> getByName(String name) {
        if (name != null){
            List<Admin> admin = adminMapper.getByName(name);
            return admin;
        }
        return null;
    }
}